package unamed.entities;

import basis.game.entities.Entity;
import basis.game.shape.Line2D;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;
import static java.lang.Math.*;

public class Lightning extends Entity{
    
    private int nPoints=5,width=5;
    
    private Line2D line;
    private double length;
    private double[] xs,ys;
    private int color=50<<16|150<<8|220;
    private Random ran;
    
    public Lightning(Line2D line){
        super(line.x1,line.y1,0,0);
        this.line=line;
        length=line.getLength();
        xs=ys=new double[nPoints];
        ran=new Random(line.hashCode()+System.currentTimeMillis());
        generate();
    }
    
    private void generate(){
        xs[0]=x;
        ys[0]=y;
        for(int i=1;i<nPoints-1;i++){
            double len=width/2-width*ran.nextDouble();
            xs[i]=x+(length/nPoints)*i-len*cos(PI/4);
            ys[i]=y+(length/nPoints)*i-len*sin(PI/4);
        }
        xs[nPoints-1]=line.x2;
        ys[nPoints-1]=line.y2;
    }
    
    @Override
    public void update(long t){
        
    }
    
    @Override
    public void render(Graphics2D g){
        g.setColor(new Color(color));
        for(int i=0;i<nPoints-1;i++){
            g.drawLine((int)xs[i],(int)ys[i],(int)xs[i+1],(int)ys[i+1]);
        }
    }
    
}